package com.qf.controller;

import com.qf.pojo.Cart;
import com.qf.pojo.User;
import com.qf.service.CarService;
import com.qf.service.impl.CarServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@WebServlet("/cart")
public class CarController extends  BaseServlet {
    public String create(HttpServletRequest request, HttpServletResponse response){
        String pid = request.getParameter("pid");
        User user=(User)request.getSession().getAttribute("loginUser");
        if(user==null){
            return "redirect:/login.jsp";
        }
        CarService carService=new CarServiceImpl();
        carService.createCart(user.getId(),Integer.parseInt(pid));
        return "redirect:/cart?method=show";
    }
    public  String show(HttpServletRequest request,HttpServletResponse response){
        User user = (User) request.getSession().getAttribute("loginUser");
        if(user==null){
            return "redirect:/login.jsp";
        }
        CarService carService=new CarServiceImpl();
        List<Cart> cartList = carService.findCartListByUid(user.getId());
        request.setAttribute("list",cartList);
        return "forward:/cart.jsp";
    }
    public String update(HttpServletRequest request,HttpServletResponse response){
        User user = (User) request.getSession().getAttribute("loginUser");
        if(user==null){
            return "redirect:/login.jsp";
        }
        String cid=request.getParameter("cid");
        String cnum = request.getParameter("cnum");
        CarService carService=new CarServiceImpl();
        carService.updateCart(Integer.parseInt(cid),Integer.parseInt(cnum));
        return "redirect:/cart?method=show";
    }
    public String delete(HttpServletRequest request,HttpServletResponse response){
        User user = (User) request.getSession().getAttribute("loginUser");
        if(user==null){
            return "redirect:/login.jsp";
        }
        String cid = request.getParameter("cid");
        CarService carService=new CarServiceImpl();
        carService.deleteById(Integer.parseInt(cid));
        return "redirect:/cart?method=show";
    }
}
